package com.blog.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.blog.lucene.BlogIndex;
import com.blog.pojo.Blog;
import com.blog.pojo.PageBean;
import com.blog.service.BlogService;
import com.blog.util.UtilResponse;
/**
 * 博客管理员层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {
	
	@Resource
	private BlogService blogService;
	// 博客索引
	private BlogIndex blogIndex=new BlogIndex();
	/**
	 * 添加或修改博客信息
	 * @param blog
	 * @param response
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Blog blog,HttpServletResponse response) throws Exception{
		int isok = 0;//操作结果
		//判断操作类型
		if(blog.getId() == null){
			isok = blogService.add(blog);//添加
			blogIndex.addIndex(blog);//添加博客索引
		}else{
			isok = blogService.update(blog);//修改
			blogIndex.updateIndex(blog);//更新博客索引
		}
		//返回结果
		JSONObject result = new JSONObject();
		if(isok > 0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 删除博客信息
	 * @param ids 多对象 xx,xx,xx...
	 *  @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(String ids,HttpServletResponse response) throws Exception{
		//解析ids
		String idsStr[] = ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			blogService.delete(Integer.parseInt(idsStr[i]));//删除博客
			blogIndex.deleteIndex(idsStr[i]);//删除博客索引
		}
		//返回结果
		JSONObject result = new JSONObject();
		result.put("success", true);
		UtilResponse.write(response, result);
		return null;
	}
	
	/**
	 * 根据id查询博客实体信息
	 * @param id
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/findById")
	public String findById(int id,HttpServletResponse response) throws Exception{
		Blog blog = blogService.findById(id);//查询实体
		//返回结果
		JSONObject result = new JSONObject();
		if(blog != null){
			result.put("success", true);
			result.put("blog", blog);
		}else{
			result.put("success", false);
		}
		System.out.println(result);
		UtilResponse.write(response, result);
		
		return null;
	}
	
	/**
	 * 分页查询博客信息
	 * @param page
	 * @param rows
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/list")
	public String list(int page,int rows,HttpServletResponse response) throws Exception{
		//分页bean
		PageBean pageBean = new PageBean(page, rows);
		//分页查询条件
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		//获取集合
		List<Blog> list = blogService.list(map);
		//获取总数 -- 此处map条件无用
		Long total = blogService.getTotal(map);
		//JSON
		JSONObject result = new JSONObject();
		result.put("rows", list);
		result.put("total", total);
		System.out.println(result);
		UtilResponse.write(response, result);
 		return null;
	}
}
